package com.shqcjd.sinopecdemomobile.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.pojo.EquipmentInspectionBean;
import com.shqcjd.sinopecdemomobile.ui.activity.preview.PreImageActivity;
import com.shqcjd.sinopecdemomobile.ui.adapter.EquipmentRecordAbnormalAdapter;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.Config;

import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class EquipmentInspectionRecordActivity extends BaseActivity {
    @BindView(R.id.tv_include_title)
    TextView tvIncludeTitle;
    @BindView(R.id.tv_include_finish)
    Button tvIncludeFinish;
    @BindView(R.id.ll_equipment_inspection)
    LinearLayout llEquipmentInspection;
    @BindView(R.id.sv_equment_inspection)
    ScrollView svEqumentInspection;


    private Context context;
    private int tId;
    private String kks = "";





    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_equipment_inspection_record;
    }

    @Override
    public void init() {
        super.init();
        tId = getIntent().getIntExtra("tId", 1);
        kks = getIntent().getStringExtra("kks");
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void initView() {
        super.initView();
        context = this;
        tvIncludeTitle.setText("巡检记录");
        tvIncludeFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

            }
        });

        getData();
    }

    EditText edChildValue = null;
    private int position = 0;

    private void initListView(List<EquipmentInspectionBean.PartsBean> mList) {
        for (EquipmentInspectionBean.PartsBean p : mList) {
            position=0;
            View v = LayoutInflater.from(context).inflate(R.layout.item_equipment_inspect_title, null);
            TextView title = v.findViewById(R.id.tv_title);
            title.setText(p.part_name);
            llEquipmentInspection.addView(v);
            for (EquipmentInspectionBean.PartsBean.ItemsBean c : p.items) {
                position++;
                View vChild;
                TextView childTitle;
                TextView chileStandard;
                TextView childState;
                TextView childCode;
                TextView tv_child_abnormal;
                RadioButton childTrue, childFalse;
                LinearLayout ll_img;
                LinearLayout ll_right;
                RadioGroup childRg;
                if (c.check_method.size() == 0) {
                    if(c.check_type!=3){
                        vChild = LayoutInflater.from(context).inflate(R.layout.item_equipment_inspect_common_record, null);
                    }else{
                        vChild = LayoutInflater.from(context).inflate(R.layout.item_equipment_inspect_number_record, null);
                        edChildValue = vChild.findViewById(R.id.ed_child_value);
                        if(!c.value.equals("-1")){
                            edChildValue.setText(c.value+"");
                        }
                        edChildValue.setTag(c.id);
                    }
                } else {
                    if (c.check_type == 1) {
                        vChild = LayoutInflater.from(context).inflate(R.layout.item_equipment_inspect_color_record, null);
                    } else {
                        vChild = LayoutInflater.from(context).inflate(R.layout.item_equipment_inspect_number_record, null);
                        edChildValue = vChild.findViewById(R.id.ed_child_value);
                        if(!c.value.equals("-1")){
                            edChildValue.setText(c.value+"");
                        }
                        edChildValue.setTag(c.id);
                    }

                }
                childTitle = vChild.findViewById(R.id.tv_child_title);
                chileStandard = vChild.findViewById(R.id.tv_child_standard);
                childState = vChild.findViewById(R.id.tv_child_state);
                childTrue = vChild.findViewById(R.id.rb_child_state_true);
                childFalse = vChild.findViewById(R.id.rb_child_state_false);
                childCode = vChild.findViewById(R.id.tv_child_code);
                ll_img = vChild.findViewById(R.id.ll_img);
                ll_right = vChild.findViewById(R.id.ll_right);
                tv_child_abnormal = vChild.findViewById(R.id.tv_child_abnormal);
                childRg = vChild.findViewById(R.id.rg_child_state);

                if(c.check_type==3){
                    childRg.setVisibility(View.INVISIBLE);
                }
                ViewGroup.LayoutParams lp;
                lp=ll_right.getLayoutParams();
                lp.width= (int) (Config.mWidth*0.81);
                ll_right.setLayoutParams(lp);
                if(c.imgs.size()>0){
                    ll_img.setVisibility(View.VISIBLE);
                    RecyclerView rv_child_abnormal = vChild.findViewById(R.id.rv_child_abnormal);
                    rv_child_abnormal.setLayoutManager(new GridLayoutManager(context, 3));
                    EquipmentRecordAbnormalAdapter mAdapter = new EquipmentRecordAbnormalAdapter(c.imgs, context);
                    rv_child_abnormal.setAdapter(mAdapter);
                    mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            Intent it = new Intent(context, PreImageActivity.class);
                            it.putExtra("url", adapter.getData().get(position).toString());
                            startActivity(it);
                        }
                    });
                }

                tv_child_abnormal.setText(c.description);
                childCode.setText(position+"");
                childTitle.setText(c.name);
                if(c.last_result==1){
                    childState.setText("正常");
                }else if(c.last_result==2){
                    childState.setText("异常");
                }
                chileStandard.setText(c.stand);
                if(c.result==1){
                    childTrue.setChecked(true);
                }else if(c.result==2){
                    childFalse.setChecked(true);
                }
                llEquipmentInspection.addView(vChild);
            }
        }
    }



    private void getData() {
        ApiService.equipmentInspectionHttp(tId, kks.replace("#", "%23"))
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<EquipmentInspectionBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在加载...");
                    }

                    @Override
                    public void _onNext(ResponseData<EquipmentInspectionBean> responseData) {
                        if (responseData.retCode != 10000) {
                            return;
                        }

                        initListView(responseData.data.parts);
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        hideWaitingDialog();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
